<template>
	<theme-box title="电子券添加">
		<form class="form-inline" @submit.prevent v-el:form>
			<form-select title="一键优惠券模板"   :options="modList" :value.sync='modVal' style="margin-left:8px;"></form-select>
			<div class="box-body">
				<form-select title="商户号" :required="true" name="mchtId" :options="mchtList" :value.sync="op"></form-select>
				<span v-show="ok">
				<form-input title="电子券名称" :required="true" name="name" :value="info.name"></form-input>
				<form-checkbox title="适用商户" name="mchtIds" :options="mchtsCheckboxOptions" :value.sync="mchtIds" v-show="mchts"></form-checkbox>
				<form-select title="类型" :required="true" name="type" :options="type" :value.sync="tpeVal" ></form-select>
				<form-input title="抵用券限制金额" :required="true" name="restricted" class="restricted" v-show="tpeVal =='voucher'" :value.sync ="voucherNum" :value="info.restricted"></form-input>
				<form-select title="发券途径" :required="true" name="promAppro" :options="sendType" :value.sync="sendVal"></form-select>
				<form-input title="现金" :required="true" name="cash" v-show="sendVal =='buy'"></form-input>
				<form-select title="电子券使用状态" :required="false" name="type2" :options="useState" id="type2" :value="info.type2"></form-select>
				<form-select title="电子券使用状态" :required="false" name="type3" :options="useState3" id="type3"></form-select>
				<form-select title="电子券使用状态" :required="false" name="type4" :options="useState4" id="type4"></form-select>
				<form-checkbox title="不可用日期" name="disableDate" :options="unavailable" id="date" :value="checkVal"></form-checkbox>
				<form-input title="面值" :required="true" name="denomination" v-show="tpeVal != 'rebate'" :value.sync ="denNum" :value="info.denomination"></form-input>
				<form-input title="面值(90代9折)" :required="true" name="denominationTwo" v-show="tpeVal == 'rebate'" :value="info.denominationTwo"></form-input>
				<form-input title="总数量" :required="true" name="num" :value.sync="num" :value="info.num">
					<div id="message">还剩<span>{{over}}</span>张可发放</div>
				</form-input>
				<form-checkbox title="使用规则" :required="false" name="useRule" :options="ruleOptions" :value='useVal' id="rule"></form-checkbox>
				<form-input title="叠加张数" :required="true" name="useNum" id="useNum" :value="info.useNum"></form-input>
				<form-select title="适用范围" :required="true" name="scpoe" :options="range" :value.sync="scpoeValue" id="scpoe" v-show="this.rep != '4'"></form-select>
					<form-checkbox title="适用范围选项" :required="true" name="range" :options="checkboxOptions" v-show="box" :value.sync="rangeValue" id="choiceRange"></form-checkbox>
				<form-select title="第三方券类型" :required="true" name="couponType" :options="type2" :value.sync="thirdTpe"></form-select>
				<form-select title="使用描述类型" :required="thirdTpe == 'third'" v-if="thirdTpe == 'third'" name="thirdType" :options="thirdUseType" :value.sync="thirdUseTpe"></form-select>
				<form-textarea title="第三方描述" :required="thirdTpe == 'third'"  name="thirdDesc"></form-textarea>
				<form-select title="使用开始时间" :required="true" name="useTime" :options="startTime" :value="info.useTime"></form-select>
				<form-select title="使用结束时间" :required="true" name="useEndTime" :options="startTime" :value="info.useEndTime"></form-select>
				<form-select title="使用开始时间2" :required="false" name="useTime2"  :options="startTime2"></form-select>
				<form-select title="使用结束时间2" :required="false" name="useEndTime2"  :options="startTime2"></form-select>
				<form-date-time  title="有效开始日期" :required="true" name="validatyBegin" :value="begDate" :read-only="true"></form-date-time>
					<!--<div style="display:inline-block;width:100%;hieght:30px"></div>-->
				<form-date-time  title="有效结束日期" :required="true"  name="validatyEnd" :value="endDate" :read-only="true"></form-date-time>
			</span>
			</div>
			<div class="box-footer">
				<v-button type="reset" @click="reset">重置</v-button>
				<v-button type="submit" theme="primary" class="pull-right">确认</v-button>
			</div>
		</form>
	</theme-box>
</template>

<style media="screen" scoped>
	.col-sm-10.item > div > label {
		width: 200px;
	}
	.form-group .restricted{
		display: block;
	}
	.adapt .col-sm-10.item > div > label {
		width: auto;
	}
	#message{
		position: absolute;
		right:0;
	}
</style>

<script>
    import 'plugins/jquery-validation';
    import {
        dateTime,
        createDataTable
    } from 'ui/collections/general.js'
    var serialize = require('form-serialize');
    import {dateFormat} from "lib/method.js";
    import {rebate_one,rebate_two,voucher_one,voucher_two,cash_one,cash_two} from 'lib/mod.js';

    var dataTable;
    var columnsSet;
    var timeAll;
    export default {
        data: () => ({
            modVal:'',//选中哪个模板
            info:{},
            begDate:'',
            endDate:'',
			checkVal:[],
            useVal:[],
            op: "0",
            ok: false,
            judge:'false',
            rep:'',
            thirdVal:'',
            thirdTpe:'',
            thirdUseTpe:'',
            couponType:'',
            sendVal:'',
            mchtId:'',
            rangeValue:[],
            scpoeValue:"",
            mchtIds:[],
            box:true,
            mchts:true,
            voucherNum:"",
            voucherMoney:"",
            denNum:"",
            eleNum:"",
            over:"",
            mchtIdValue:"",
            tpeVal:"",
            num:"",
            lowTypeNameList:[],
            mchtList: [],
            mchtsList:[],
            checkList:[],
//			ruleList:[],
            checkboxOptions:[],
            mchtsCheckboxOptions:[],
            modList:[
                {key: "折扣券模板一",value: "rebate_one"},
                {key: "折扣券模板二",value: "rebate_two"},
                {key: "抵用券模板一",value: "voucher_one"},
                {key: "抵用券模板二",value: "voucher_two"},
                {key: "代金券模板一",value: "cash_one"},
                {key: "代金券模板二",value: "cash_two"}
            ],
            type:[
                {key: "折扣券",value: "rebate"},
                {key: "抵用券",value: "voucher"},
                {key: "代金券",value: "cash"},
                {key: "礼品券",value: "present"},
                {key: "票务",value: "ticket"},
            ],
            type2:[
                {key: "默认",value: "default"},
                {key: "第三方",value: "third"}
            ],
            thirdUseType:[
                {key: "默认",value: "default"},
                {key: "链接",value: "url"}
			],
            useState:[
                {key: "出售、分享、使用",value: "default"},
                {key: "分享、使用",value: "share"},
                {key: "使用",value: "useonly"},
            ],
            useState3:[
                {key: "分享、使用",value: "share"},
                {key: "使用",value: "useonly"},
            ],
            useState4:[
                {key: "使用",value: "useonly"}
            ],
            range:[
                {key:"可用",value:"I"},
                {key:"不可用",value:"T"},
            ],
            sendType:[
                {key: "普通券",value: "general"},
                // {key: "推广券",value: "prom"},
                // {key: "打赏券",value: "reward"},
                {key: "购买",value: "buy"}
            ],
            unavailable:[
                {
                    key: "星期一",
                    value: "1"
                }, {
                    key: "星期二",
                    value: "2"
                }, {
                    key: "星期三",
                    value: "3"
                }, {
                    key: "星期四",
                    value: "4"
                }, {
                    key: "星期五",
                    value: "5"
                }, {
                    key: "星期六",
                    value: "6"
                }, {
                    key: "星期天",
                    value: "7"
                }],
            ruleOptions: [{
                key: "和其他优惠券一起使用",
                value: "D"
            }, {
                key: "叠加使用",
                value: "M"
            }],
            startTime:[{
                key: "00:00",
                value: "00:00"
            }, {
                key: "01:00",
                value: "01:00"
            }, {
                key: "02:00",
                value: "02:00"
            }, {
                key: "03:00",
                value: "03:00"
            }, {
                key: "04:00",
                value: "04:00"
            }, {
                key: "05:00",
                value: "05:00"
            }, {
                key: "06:00",
                value: "06:00"
            }, {
                key: "07:00",
                value: "07:00"
            }, {
                key: "08:00",
                value: "08:00"
            }, {
                key: "09:00",
                value: "09:00"
            }, {
                key: "10:00",
                value: "10:00"
            }, {
                key: "11:00",
                value: "11:00"
            }, {
                key: "12:00",
                value: "12:00"
            }, {
                key: "13:00",
                value: "13:00"
            }, {
                key: "14:00",
                value: "14:00"
            }, {
                key: "15:00",
                value: "15:00"
            }, {
                key: "16:00",
                value: "16:00"
            }, {
                key: "17:00",
                value: "17:00"
            }, {
                key: "18:00",
                value: "18:00"
            }, {
                key: "19:00",
                value: "19:00"
            }, {
                key: "20:00",
                value: "20:00"
            }, {
                key: "21:00",
                value: "21:00"
            }, {
                key: "22:00",
                value: "22:00"
            },{
                key: "23:00",
                value: "23:00"
            }],
            startTime2:[ {
                key: " ",
                value: " "
            },{
                key: "00:00",
                value: "00:00"
            }, {
                key: "01:00",
                value: "01:00"
            }, {
                key: "02:00",
                value: "02:00"
            }, {
                key: "03:00",
                value: "03:00"
            }, {
                key: "04:00",
                value: "04:00"
            }, {
                key: "05:00",
                value: "05:00"
            }, {
                key: "06:00",
                value: "06:00"
            }, {
                key: "07:00",
                value: "07:00"
            }, {
                key: "08:00",
                value: "08:00"
            }, {
                key: "09:00",
                value: "09:00"
            }, {
                key: "10:00",
                value: "10:00"
            }, {
                key: "11:00",
                value: "11:00"
            }, {
                key: "12:00",
                value: "12:00"
            }, {
                key: "13:00",
                value: "13:00"
            }, {
                key: "14:00",
                value: "14:00"
            }, {
                key: "15:00",
                value: "15:00"
            }, {
                key: "16:00",
                value: "16:00"
            }, {
                key: "17:00",
                value: "17:00"
            }, {
                key: "18:00",
                value: "18:00"
            }, {
                key: "19:00",
                value: "19:00"
            }, {
                key: "20:00",
                value: "20:00"
            }, {
                key: "21:00",
                value: "21:00"
            }, {
                key: "22:00",
                value: "22:00"
            },{
                key: "23:00",
                value: "23:00"
            }]
        }),
        watch:{
            async tpeVal(value){
                this.couponType = value;
                if(this.sendValue != 'buy'){
                    $("#type4").css('display','none');
                    if(this.couponType == 'rebate' || this.couponType == 'cash' || this.couponType == 'voucher'){
                        $("#type3").css('display','none');
                        $("#type2").css('display','');
                    }else{
                        $("#type3").css('display','');
                        $("#type2").css('display','none');
                    }
                }
                if(value == 'rebate' || value == 'present' || value == 'ticket'){
                    $("#rule").css("display","none");
                    $("#useNum").css("display","none");
                }else{
                    //抵用券和代金券显示使用规则
                    $("#rule").css("display","");
                }
                if(value == 'present' || value == 'ticket'){
                    $("#scpoe").css("display","none");
                    $("#choiceRange").css("display","none");
                }else{
                    if(this.rep != '4'){
                        $("#scpoe").css("display","");
                        $("#choiceRange").css("display","");
                    }
                }
                //抵用券和代金券的时候 面值可以是保留一位的小数
                // if(value == 'voucher' || value == 'cash'){
                //    $(this.$els.form.denomination).rules("remove", "int");
                //    $(this.$els.form.denomination).rules("add", {
                //        maxlength: 5
                //    });
                // }else{
                //    $(this.$els.form.denomination).rules("remove", "maxlength");
                // }
            },

            //请求适用范围
            op(v){
                let self = this;
                this.mchtId = v;
                //请求商户标识
                if(v != '') {
                    self.$post({
                        url: "/api/v2" + projectName_Url + "/plat/coupon/getMchtIdent/" + v,
                        method: "GET"
                    }).then(response => {
                        self.rep = response.content.mchtIdent
                        //请求菜单类型
                        //商圈商户默认全适用
                        self.$post({
                            url: "/api/v2" + projectName_Url + "/plat/dishType/querylow/" + v,
                            method: "GET"
                        }).then(response => {
                            this.lowTypeNameList = response.content.lowTypeNameList;
                            self.$set("checkList", this.lowTypeNameList)
                            this.lowTypeNameList.length > 0 ? self.box = true : self.box = false;
                            var _array = [];
                            for (let i = 0; i < this.lowTypeNameList.length; i++) {
                                _array.push({
                                    value: this.lowTypeNameList[i].typeId,
                                    key: this.lowTypeNameList[i].typeName
                                });
                            }
                            if (self.rep != '4') {
                                if (this.lowTypeNameList == "") {
                                    Midway.alert({title: "操作提示", content: '请先添加商品类型'}).then(function () {
                                        self.$route.router.go('/cuisine/menuCreate');
                                    })
                                } else {
                                    self.checkboxOptions = _array;
                                }
                            } else {
                                self.checkboxOptions = [];
                                self.checkboxOptions.length > 0 ? self.box = true : self.box = false;
                            }

                            //不可用不能全选
                            var length = this.lowTypeNameList.length;
                            $("#scpoe").change(function () {
                                $("#choiceRange").click(function () {
                                    if ($("#scpoe").val() == 'T') {
                                        if ($("input[name = 'range']:checked").length >= length) {
                                            if (self.rep != '4') {
                                                Midway.alert({title: "操作提示", content: "至少选一个可用"})
                                                event.srcElement.checked = false;
                                            }
                                        }
                                    }
                                })
                            });
                            if (self.rep != '4') {
                                //请求适用商户
                                self.$post({
                                    url: "/api/v2" + projectName_Url + "/plat/mcht/getmchtname/" + v,
                                    method: "GET"
                                }).then(response => {
                                    let list = response.content.info;
                                    self.$set("mchtsList", list)
                                    list.length > 0 ? self.mchts = true : self.mchts = false;
                                    var _array = [];
                                    for (let i = 0; i < list.length; i++) {
                                        _array.push({value: list[i].mchtId, key: list[i].mchtName});
                                    }
                                    self.mchtsCheckboxOptions = _array;
                                }, error => {
                                })
                            } else {
                                //请求适用商圈
                                self.$post({
                                    url: "/api/v2" + projectName_Url + "/plat/mcht/gettdareamchtname/" + v,
                                    method: "GET"
                                }).then(response => {
                                    let list = response.content.info;
                                    self.$set("mchtsList", list)
                                    list.length > 0 ? self.mchts = true : self.mchts = false;
                                    var _array = [];
                                    for (let i = 0; i < list.length; i++) {
                                        _array.push({value: list[i].mchtId, key: list[i].mchtName});
                                    }
                                    self.mchtsCheckboxOptions = _array;
                                }, error => {
                                })
                            }
                        }, error => {
                        });

                    }, error => {
                    })

                    //判断电子券发行数量
                    this.$post({
                        url: '/api/v2' + projectName_Url + '/plat/issue/' + v,
                        method: "POST"
                    }).then(response => {
                        this.eleNum = response.content;
                        this.over = this.eleNum;
                        if (this.eleNum <= 0) {
                            $("#num").attr("disabled", true);
                        } else {
                            $("#num").attr("disabled", false);
                        }

                    });
                }
                v == "" ? this.ok = false : this.ok = true
                setTimeout(()=>{
                    dateTime({
                        // startDate:'',
                        // endDate:'',
                        format: "yyyy-mm-dd",
                        minView: "2",
                    });
                },500)
            },
            //适用范围选择后取相反的值进行显示
            scpoeValue(value){
                let set = [];
                set = this.checkboxOptions.map(data => {
                    return data.value
                });
                let dir = _.difference(set,this.rangeValue);
                this.rangeValue = dir;
                //监听直接点击不可用
                if(value == 'T'){
                    if(this.rep != '4'){
                        if(this.rangeValue.length >= this.lowTypeNameList.length){
                            Midway.alert({title:"操作提示",content:"至少选一个可用"})
                        }
                    }
                }
            },
            //判断电子券剩余数量
            num(v){
                if(v > this.eleNum){
                    Midway.alert({title: "操作提示", content: '您可发行的最大数量为' + this.eleNum})
                }
                this.over = this.eleNum - v;
            },
            //判断抵用券金额大于面值
            voucherNum(v){
                this.voucherMoney = v;
            },

            sendVal(v){
                this.sendValue = v;
                if(v == 'buy'){
                    $("#type2").css('display','none');
                    $("#type3").css('display','none');
                    $("#type4").css('display','');
                }else{
                    $("#type4").css('display','none');
                    if(this.couponType == 'rebate' || this.couponType == 'cash' || this.couponType == 'voucher'){
                        $("#type3").css('display','none');
                        $("#type2").css('display','');
                    }else{
                        $("#type3").css('display','');
                        $("#type2").css('display','none');
                    }
                }
            },
            thirdTpe(v){
                this.thirdVal = v;
                if (v == "third") {
                    this.judge = true;
                    $(this.$els.form.thirdDesc).rules("add", {
                        required:true,
                        maxlength: 300
                    });
                } else {
                    this.judge = false;
                    $(this.$els.form.thirdDesc).rules("remove", "maxlength");
                    $(this.$els.form.thirdDesc).rules("remove", "required");
                }
            },
            thirdUseTpe(v){
                $(this.$els.form.thirdDesc).val('');
                if (v == "url") {
                    this.judge = true;
                    $(this.$els.form.thirdDesc).rules("add", {
                        required:true,
						url:true,
                        maxlength: 300
                    });
                } else {
                    this.judge = false;
                    if(this.thirdVal != "third"){
                        $(this.$els.form.thirdDesc).rules("remove", "maxlength");
                        $(this.$els.form.thirdDesc).rules("remove", "required");
					}
                    $(this.$els.form.thirdDesc).rules("remove", "url");
                }
            },
            //监听选择哪个模板
            modVal(v){
                //不可用日期六日
                this.checkVal = ['6','7']
                //开始日期默认当前日期
               //获取当前时间戳
                var d= new Date();
                var d2 = d.getFullYear() + "-"+ (d.getMonth() + 1) +"-"+ d.getDate();
                //月是一位数时自动补齐0
                if((d.getMonth() + 1) < 10){
                      var d2 = d.getFullYear() + "-"+ "0"+(d.getMonth() + 1) +"-"+ d.getDate();
                }
                //日是一位数时自动补齐0
                if(d.getDate() < 10){
                      var d2 = d.getFullYear() + "-"+ "0"+(d.getMonth() + 1) +"-"+"0"+d.getDate();
                }
                this.begDate = d2;
                //获取下一个月
                var next_d2 = d.getFullYear() +"-"+ (d.getMonth()+2) +"-"+ d.getDate();
                 //月是一位数时自动补齐0
                if((d.getMonth() + 1) < 10){
                      var next_d2 = d.getFullYear() + "-"+ "0"+(d.getMonth() + 2) +"-"+ d.getDate();
                }
                //日是一位数时自动补齐0
                if(d.getDate() < 10){
                      var next_d2 = d.getFullYear() + "-"+ "0"+(d.getMonth() + 2) +"-"+"0"+d.getDate();
                }
                this.endDate = next_d2;
                //调用对应的模板
                if(v == 'rebate_one'){
                    this.info = rebate_one;
                    this.tpeVal = "rebate";
                }else if(v == 'rebate_two'){
                    this.info = rebate_two;
                    this.tpeVal = "rebate";
                }else if(v == 'voucher_one'){
                    this.info = voucher_one;
                    this.tpeVal = "voucher";

                }else if(v == 'voucher_two'){
                    this.info = voucher_two;
                    this.tpeVal = "voucher";

                }else if(v == 'cash_one'){
                    this.info = cash_one;
                    this.tpeVal = "cash";

                }else{
                    this.info = cash_two;
                    this.tpeVal = "cash";
                    //    $("select[name='type'] option:eq(2)").prop("selected","selected")
                }
                //叠加使用默认被选中
                if(this.tpeVal == "cash" || this.tpeVal == "voucher"){
                    this.useVal = ['M']
                    $("#useNum").css("display","");

                }
            }
        },
        methods: {
            reset() {
                $("label.error").remove();
                this.$els.form.reset();
                this.op = "";
                //请求适用商户
                // this.$post({
                //     url: "/api/v2" + projectName_Url + "/plat/mcht/getmchtname/"+ this.$els.form.mchtId.value,
                //     method:"GET"
                // }).then(response => {
                //     let list = response.content.info;
                //     this.$set("mchtsList", list)
                //     list.length > 0 ? this.mchts =true:this.mchts=false;
                //     var _array = [];
                //     for(let i=0;i<list.length;i++){
                //         _array.push({value:list[i].mchtId,key:list[i].mchtName});
                //     }
                //     this.mchtsCheckboxOptions = _array;
                // }, error => {
                // });
                //请求适用范围
                if(this.mchtIdent != '4'){
                    // this.$post({
                    //     url: "/api/v2" + projectName_Url + "/plat/dishType/querylow/"+ this.$els.form.mchtId.value,
                    //     method:"GET"
                    // }).then(response => {
                    //     let list = response.content.lowTypeNameList;
                    //     this.$set("checkList", list)
                    //     list.length > 0 ? self.box = true : self.box = false;
                    //     var _array = [];
                    //     for (let i = 0; i < list.length; i++) {
                    //         _array.push({value: list[i].typeId, key: list[i].typeName});
                    //     }
                    //     if (list == "") {
                    //         Midway.alert({title: "操作提示", content: '请先添加商品类型'}).then(function () {
                    //             self.$route.router.go('/cuisine/menuCreate');
                    //         })
                    //     } else {
                    //         this.checkboxOptions = _array;
                    //     }
                    // })
                }
            },
            getMcht(data) {
                let self = this;
                var mchtId = JSON.parse(localStorage.getItem("id_token")).mchtId
                self.$post({
                    url: "/api/v2" + projectName_Url + "/list/mcht/"+ mchtId,
                    method:"GET"
                }).then(response => {
                    let list = response.content.info;
                    list.unshift({key:"请选择商户",value:""});
                    this.mchtList = list;
                    self.$set("mchtList", list);
                    self.op = list[0].value
                    let _value = []
                    for(var i = 0;i < list.length;i++){
                        _value.push(list[i].value)
                    }
                }, error => {});
            }
        },
        ready: function() {
            let self = this;
            self.getMcht();
            //点击叠加使用  叠加张数显示
            $("#rule .checkbox-item label:nth-child(2)>input").click(function(){
                if($("#rule .checkbox-item label:nth-child(2)>input").prop("checked")){
                    $("#useNum").css("display","");
                }else{
                    $("#useNum").css("display","none");
                }
            });
            //不可用日期 不能全选
            $("#date").click(function(){
                if($("input[name = 'disableDate']:checked").length > 6){
                    Midway.alert({title:"操作提示",content:"电子券使用日期至少有一天可用"})
                    event.srcElement.checked=false;
                }
            });
            $(this.$els.form).validate({
                submitHandler: function(form) {
                    var _data = serialize(this.currentForm, {
                        hash: true,
                        empty: true
                    });
                    //useTime进行数据处理
                    var useTime = $("#useTime").val()+'-'+$("#useEndTime").val();
                    _data.useTime = useTime

                    //进行不可使用日期的数据处理
                    var disableDate = [];
                    for (var key in _data.disableDate) {
                        if (_data.disableDate[key]) {
                            disableDate.push(_data.disableDate[key]);
                        }
                    }

                    //判断营业时间段1不能都为0
                    if(self.$els.form.useTime.value == "00:00" && self.$els.form.useEndTime.value == "00:00" ){
                        Midway.alert({title: "操作提示", content: '使用时间段不能都为00:00'})
                        return false
                    }

                    //对有效起始结束日期进行数据处理
                    _data.validatyBegin = _data.validatyBegin.replace(/-/g, "");
                    _data.validatyEnd = _data.validatyEnd.replace(/-/g, "");

                    // //判断电子券有效期不低于一天
                    // if(_data.promAppro != 'buy'&&_data.cash != '0'){
                    //     var lastData = _data.validatyEnd-_data.validatyBegin;
                    //     var time = 1;
                    //     if(_data.validatyBegin.length>0 && _data.validatyEnd.length>0){
                    //         if(lastData<0||lastData<time){
                    //             Midway.alert({title: "操作提示", content: '有效开始和有效结束日期相差需大于一天'})
                    //             return false
                    //         }
                    //     }
                    // }
                    //判断营业时间段2
                    if (self.$els.form.useTime2.value != " " || self.$els.form.useEndTime2.value != " ") {
                        if (self.$els.form.useTime2.value != " " && self.$els.form.useEndTime2.value != " ") {
                            if (self.$els.form.useTime2.value == "00:00" && self.$els.form.useEndTime2.value == "00:00") {
                                Midway.alert({title: "操作提示", content: '使用时间段2,不能都为00:00'})
                                return false
                            }else if(self.$els.form.useTime2.value >= self.$els.form.useEndTime2.value){
                                Midway.alert({title: "操作提示", content: '使用时间段2,开始时间不能大于等于结束时间'})
                                return false
                            }else{
                                var useTime2 = $("#useTime2").val() + '-' + $("#useEndTime2").val();
                                _data.useTime = useTime + ',' + useTime2;
                            }
                        }else{
                            self.$els.form.useTime2.value != ' ' && self.$els.form.useEndTime2.value != ' ';
                            Midway.alert({title: "操作提示", content: '使用时间段2,需填写完整'})
                            return false

                        }
                    }
                    //判断两个时间段不能重合
                    var y = self.$els.form.useEndTime.value;
                    var a = self.$els.form.useTime2.value;
                    var b = self.$els.form.useEndTime2.value;
                    var x = self.$els.form.useTime.value;
                    if(y <= a || b <= x){

                    }else{
                        Midway.alert({title: "操作提示", content: '两个时间段不能有重合'})
                        return false
                    };
                    //发行数量验证
                    if (_data.num > self.eleNum) {
                        Midway.alert({title: "操作提示", content: "您可发行的最大数量为：" + self.eleNum})
                        return false
                    };
                    //抵用券金额与面值验证
                    if(_data.type == 'voucher'){
                        if (Number(_data.denomination)> Number(_data.restricted)) {
                            Midway.alert({title: "操作提示", content:'面值应小于抵用券金额'})
                            return false
                        }else{

                        };
                    }
                    //不可用不能全选验证
                    if(_data.scpoe == "T"){
                        if (self.rangeValue.length >= self.lowTypeNameList.length ) {
                            if(self.rep != '4'){
                                Midway.alert({title: "操作提示", content:'至少选一个可用'})
                                return false
                            }
                        };
                    }
                    //适用范围不必填时 如果不选 传I
                    if(_data.type == 'present' || _data.type == 'ticket'){
                        if(self.rangeValue.length == 0){
                            _data.scpoe = 'I'
                        }
                    }
                    //使用规则 数据处理
                    var useRule = [];
                    for (var key in _data.useRule) {
                        if (_data.useRule[key]) {
                            useRule.push(_data.useRule[key]);
                        }
                    }
                    delete _data.useRule;
                    _data.useRule = useRule.join("");
                    //适用范围数据处理
                    // var _set =new Set(_data.range);
                    // if(_set.has("")){
                    //     _set.delete("");
                    // }
                    var _rangeArr=Array.from(self.rangeValue);
                    var _check =  _data.scpoe;     //[I T]
                    var _newStr = _check+_rangeArr.join(",");
                    _data.scpoe = _newStr

                    //适用商户数据处理
                    // var _set =new Set(_data.mchtIds);
                    // if(_set.has("")){
                    //     _set.delete("");
                    // }
                    var _mchtsArr=Array.from(self.mchtIds);
                    var _newMchtsArr = "";
                    _newMchtsArr += _mchtsArr.join(",");
                    _data.mchtIds = _newMchtsArr

                    //面值字段传值
                    if(_data.type == "rebate"){
                        _data.denomination = _data.denominationTwo
                    }else{
                        _data.denomination = _data.denomination
                    }
                    if(_data.type != "voucher"){
                        _data.restricted = 0
					}else{
                        _data.restricted = _data.restricted;
					}
                    delete _data.disableDate;
                    _data.disableDate = disableDate.join(",");
                    //电子券状态传值
                    if(_data.type == "present" || _data.type == 'ticket'){
                        _data.type2 = _data.type3;
                    }else{
                        _data.type2 = _data.type2;
                    }
                    if(_data.promAppro == 'buy'){
                        _data.type2 = _data.type4;
                    }else{
                        _data.cash = 0;
                    }
                    //商圈商户
                    if(self.rep == '4'){
                        _data.scpoe = 'I';
                    }
                    self.$post({
                        url: "/api/v2" + projectName_Url + "/plat/coupon/add",
                        data:_data
                    }).then(response => {
                        let repList = response.content;
                        if(_data.promAppro == 'general'){
                            let mchtId,couponId,couponTpe,denomination,num,validatyEnd,thirdType;
                            mchtId = repList.mchtId;  couponId = repList.couponId; couponTpe = _data.type;  denomination = _data.denomination; num = _data.num; validatyEnd = _data.validatyEnd; thirdType = _data.couponType;
                            var obj = {'mchtId':mchtId,'couponId':couponId,'couponType':couponTpe,'denomination':denomination,'num':num,'validatyEnd':validatyEnd,'thirdType':thirdType}
                            localStorage.setItem('params',JSON.stringify(obj));
                            let data = {
                                title: "操作成功",
                                content: "是否绑定该电子券的营销规则?"
                            }
                            self.reset();
                            Midway.confirm(data).then(function() {
                                self.$route.router.go('/coupons/marketRule');
                            }).catch(function(){
                                self.$route.router.go('/coupons/list');
                            })
                        }else{
                            let data = {
                                title: "操作成功",
                                content: "是否进入电子券列表查看?"
                            }
                            self.reset();
                            Midway.confirm(data).then(function() {
                                self.$route.router.go('/coupons/list');
                            })
                        }
                    });
                },
                errorPlacement: function(error, element) {
                    if (element.parents('div.checkbox-item').length != 0) {
                        element.parents("div.checkbox-item").find("div.checkbox-error").append(error)
                    }else if(element.parents('div.file-item').length != 0){
                        error.insertAfter(element.parents("div.file-item"));
                    }else if(element.attr("name") == "thirdDesc"){
                        element.next().append(error)
                    }else{
                        error.insertAfter(element)
                    }
                },
                rules: {
                    mchtId: {
                        required: true
                    },
                    name: {
                        required: true,
                        maxlength:32
                    },
                    denomination: {
                        required: true,
                        number: true,
                        int: true,
                        min:1
                    },
                    denominationTwo:{
                        required: true,
                        number: true,
                        int: true,
                        min:10,
                        max:99
                    },
                    restricted:{
                        int:true,
                        min:1
                    },
                    cash:{
                        cash:true
                    },
                    num:{
                        required: true,
                        number: true,
                        int: true,
                        min:1
                    },
                    per:{
                        number: true,
                        min:1
                    },
                    file:{
                        required: true,
                    },
                    useNum:{
                        min:1,
                        max:3,
                        int:true
                    },
                    range:{
                        required:true
                    },
                    useTime:{
                        required: true,
                        comparisonSize: ["useTime", "useEndTime"]
                    },
                    useEndTime:{
                        required: true,
                        comparisonSize: ["useTime", "useEndTime"]
                    },
//                    useTime2:{
//                        comparisonSize: ["useTime2", "useEndTime2"]
//                    },
//                    useEndTime2:{
//                        comparisonSize: ["useTime2", "useEndTime2"]
//                    },
                    validatyBegin:{
                        required: true,
                        compareDate: ["validatyBegin", "validatyEnd"]
                    },
                    validatyEnd:{
                        required: true,
                        compareDate: ["validatyBegin", "validatyEnd"]
                    }
                }
            });
        }
    }
</script>
